package com.bw.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.util.CronUtils;
import com.bw.util.TaskException;
import com.bw.domain.SysDictData;
import com.bw.domain.SysJob;
import com.bw.service.SysDictDataService;
import com.bw.service.SysJobService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("job")
public class SysJobController {

    @Autowired
    private SysJobService sysJobService;
    @Autowired
    private SysDictDataService sysDictDataService;

    @GetMapping("list")
    public IPage<SysJob> list(Page page, SysJob sysJob) {
        IPage<SysJob> userVoIPage = sysJobService.selectPageVo(page, sysJob);
        return userVoIPage;
    }

    /**
     * 导出
     * @param sysJob
     * @return
     */
    @GetMapping("execl")
    public Map<String, Object> execl(SysJob sysJob) {
        Map<String, Object> map = new HashMap<>();
        List<SysJob> list = sysJobService.selectJobList(sysJob);
        EasyExcel.write("D:\\excel\\job.xlsx", SysJob.class)
                .sheet()
                .doWrite(list);
        map.put("list", list);
        map.put("msg", "成功");
        map.put("code", 200);
        return map;
    }

    /**
     * 任务调度状态修改
     *
     * @param
     * @return 结果
     */
    @PostMapping("upstatus")
    public int upstatus(@RequestBody SysJob sysJob) throws SchedulerException {
        int updatestatus = sysJobService.updatestatus(sysJob);
        return updatestatus;
    }

    /**
     * 获取分组
     *
     * @param status
     * @return
     */
    @GetMapping("getGroup")
    public Map<String, Object> getGroup(String status) {
        Map<String, Object> map = new HashMap<>();
//        System.out.println(status);
        List<SysDictData> sysJob = sysDictDataService.selectGroup(status);
        map.put("list", sysJob);
        return map;
    }

    /**
     * 获取状态
     *
     * @param status
     * @return
     */
    @GetMapping("getStatus")
    public Map<String, Object> getStatus(String status) {
        Map<String, Object> map = new HashMap<>();
        List<SysDictData> sysJob = sysDictDataService.selectGroup(status);
        map.put("list", sysJob);
        return map;
    }

    /**
     * 新增
     *
     * @param sysJob
     * @return
     * @throws SchedulerException
     * @throws TaskException
     */
    @PostMapping("add")
    public Map<String, Object> add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
        Map<String, Object> map = new HashMap<>();
        if (!CronUtils.isValid(sysJob.getCronExpression())) {
            map.put("code", 1001);
            map.put("msg", "Cron表达式不正确");
            return map;
        }
        int i = sysJobService.inserts(sysJob);
        map.put("code", 200);
        return map;
    }

    /**
     * 修改
     *
     * @param sysJob
     * @return
     * @throws SchedulerException
     * @throws TaskException
     */
    @PostMapping("updateJob")
    public Map<String, Object> updateJob(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
        Map<String, Object> map = new HashMap<>();
        if (!CronUtils.isValid(sysJob.getCronExpression())) {
            map.put("code", 1001);
            map.put("msg", "Cron表达式不正确");
            return map;
        }
        int i = sysJobService.updateJob(sysJob);
        map.put("code", 200);
        return map;
    }

    /**
     * 根据ID获取信息
     *
     * @param
     * @return 结果
     */
    @GetMapping("getupbyId")
    public Map<String, Object> getupbyId(Long id) {
        Map<String, Object> map = new HashMap<>();
        SysJob byId = sysJobService.getByIds(id);
        map.put("list", byId);
        return map;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws SchedulerException
     */
    @GetMapping("del")
    public Map<String, Object> del(String id) throws SchedulerException {
        Map<String, Object> map = new HashMap<>();
        String[] split = id.split(",");
        sysJobService.delJobByIds(split);
        map.put("msg", "删除成功");
        map.put("code", 200);
        return map;
    }

    /**
     * 立即执行
     *
     * @param sysJob
     * @return
     * @throws SchedulerException
     */
    @PostMapping("run")
    public Map<String, Object> run(@RequestBody SysJob sysJob) throws SchedulerException {
        Map<String, Object> map = new HashMap<>();
        sysJobService.run(sysJob);
        map.put("msg", "执行成功");
        map.put("code", 200);
        return map;
    }
}
